package com.teacher.pc.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.pojo.dto.term.TermAddDto;
import com.teacher.pojo.dto.term.TermGetDto;
import com.teacher.pojo.dto.term.TermUpdateDto;
import com.teacher.pojo.entity.Term;
import com.teacher.service.ITermService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-07-02
 */
@RestController
@RequestMapping("/pc/term")
public class TermController {

    @Autowired
    private ITermService iTermService;

    /**
     * 添加学期
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("添加学期 term_add")
    @PreAuthorize("hasAuthority('term_add')")
    public CommonResult<String> add(@Validated @RequestBody TermAddDto dto) {
        return iTermService.add(dto);
    }

    /**
     * 更改默认学期
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("更改默认学期 term_update")
    @PreAuthorize("hasAuthority('term_update')")
    public CommonResult<String> update(@Validated @RequestBody TermUpdateDto dto) {
        return iTermService.updateInfo(dto);
    }

    /**
     * 删除学期
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除学期 term_delete")
    @PreAuthorize("hasAuthority('term_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        iTermService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }

    /**
     * 获取学期
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("获取学期 term_get")
    @PreAuthorize("hasAuthority('term_get')")
    public CommonResult<IPage<Term>> get(TermGetDto dto) {
        Page<Term> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        return CommonResult.success(iTermService.page(page, new QueryWrapper<Term>().lambda()
                .eq(ObjectUtil.isNotNull(dto.getIsCurrent()), Term::getIsCurrent, dto.getIsCurrent())
                .like(StrUtil.isNotEmpty(dto.getTerm()), Term::getTerm, dto.getTerm())
                .like(StrUtil.isNotEmpty(dto.getYear()), Term::getYear, dto.getYear())));
    }

    /**
     * 获取全部学期
     *
     * @return
     */
    @GetMapping("/all")
    @ApiOperation("获取全部学期")
    public CommonResult<List<Term>> getAll() {
        List<Term> termList = iTermService.list();
        for (Term term : termList) {
            term.setTerm(term.getYear() + "-" + term.getTerm());
        }
        return CommonResult.success(termList);
    }

}

